//
//  RegisterViewController.m
//  leo
//
//  Created by 常艳 on 15/9/13.
//  Copyright (c) 2015年 mdx. All rights reserved.
//

#import "ResetPassViewController.h"
#import "LeoMsg.h"
#import "SyncManager.h"
#import "NextViewController.h"
@interface ResetPassViewController ()

@end

@implementation ResetPassViewController
-(void)dealloc
{
    [self.timer invalidate];
    self.timer=NULL;
    self.syncer=NULL;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.extendedLayoutIncludesOpaqueBars=YES;
    self.title=@"忘记密码";
    float x=self.phone.frame.size.height;
    UIView *phoneleftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, x)];
    self.phone.leftView=phoneleftView;
    self.phone.leftViewMode=UITextFieldViewModeAlways;
    UIView *smsleftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 20,x)];
    self.smsCode.leftView=smsleftView;
    self.smsCode.leftViewMode=UITextFieldViewModeAlways;
    self.syncer=[[SyncManager alloc] init];
    self.timeLable.hidden=YES;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.view.window==nil)
    {
        self.view=nil;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)register:(id)sender {
    [self.phone resignFirstResponder];
    [self.smsCode resignFirstResponder];
    self.registerBtn.enabled=NO;
    [self.indicator startAnimating];
    NSString *phone=[self.phone text];
    NSString *smsCode=[self.smsCode text];
    phone=[phone stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    smsCode=[smsCode stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if(phone==NULL||[phone length]==0)
    {
        NSLog(@"%@,register,phone is null or smsCode is null",Reset_Tag);
        [self showErrorMsg:@"手机号码不能为空"];
        return;
    }
    if([phone length]!=11||[Tooler checkStringBegin:phone]==NO)
    {
        NSLog(@"%@,register,phone is illegal",Reset_Tag);
        [self showErrorMsg:@"手机号码无效，请输入正确的手机号码"];
        return;
    }
    if(smsCode==NULL||[smsCode length]==0)
    {
        NSLog(@"%@,register,phone is null or smsCode is null",Reset_Tag);
        [self showErrorMsg:@"验证码不能为空"];
        return;
    }
    //发送验证码
    if(self.syncer==NULL)
    {
        NSLog(@"%@,register,syncer is null",Reset_Tag);
        [self showErrorMsg:@"程序发生错误，code is 40"];
        return;
    }
    [self.syncer verifySmsCode:phone smsCode:smsCode completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        [self.indicator stopAnimating];
        self.registerBtn.enabled=YES;
        if(result==SUCESS)
        {
            //验证码正确，跳转到下一个界面
            NextViewController *nextViewController=[[NextViewController alloc]  init];
            nextViewController.phoneNum=phone;
            [self.navigationController pushViewController:nextViewController animated:YES];
            [self.timer invalidate];
            self.timeLable.hidden=YES;
            [self.smsButton setTitle:@"短信获取" forState:UIControlStateNormal];
            self.smsButton.enabled=YES;
        }
        else
        {
            [Tooler showHttpMsg:result description:description msg:@"验证码验证失败"];
        }
    }];
}
- (IBAction)getSmsCode:(id)sender {
    [self.indicator startAnimating];
    self.smsButton.enabled=NO;
    NSString *phone=[self.phone text];
    if(phone==NULL)
    {
        NSLog(@"%@,getSmsCode,phone is null",Reset_Tag);
        [self.indicator stopAnimating];
        self.smsButton.enabled=YES;
        [LeoMsg showMsg:@"请输入手机号码"];
        return;
    }
    phone=[phone stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if([phone length]==0)
    {
        NSLog(@"%@,getSmsCode,phone is null",Reset_Tag);
        [self.indicator stopAnimating];
        self.smsButton.enabled=YES;
        [LeoMsg showMsg:@"请输入手机号码"];
        return;
    }
    if([phone length]!=11)
    {
        NSLog(@"%@,getSmsCode,phone is null",Reset_Tag);
        [self.indicator stopAnimating];
        self.smsButton.enabled=YES;
        [LeoMsg showMsg:@"手机号码长度不合法"];
        return;
    }
    if(self.syncer==NULL)
    {
        NSLog(@"%@,getSmsCode,syncer is null",Reset_Tag);
        [self.indicator stopAnimating];
        self.smsButton.enabled=YES;
        [LeoMsg showMsg:@"程序发生错误，code is 41"];
        return;
    }
    [self.syncer getCheckCode:phone type:1 completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        [self getSmsCallback:result retValue:retValue description:description];
    }];
}
-(void)getSmsCallback:(SyncResult) result retValue:(NSDictionary *)ret description:(NSString *)description
{
    [self.indicator stopAnimating];
    if(result==SUCESS)
    {
        NSLog(@"%@,getSmsCallback,success",Reset_Tag);
         self.smsButton.backgroundColor=[UIColor grayColor];
        //进行倒计时
        self.seconds=60;
        NSString *timestr=[NSString stringWithFormat:@"%d           ",self.seconds];
        [self.smsButton setTitle:timestr forState:UIControlStateNormal];
        self.timeLable.hidden=NO;
        self.timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeCutdown) userInfo:nil repeats:YES];
    }
    else
    {
        NSLog(@"%@,getSmsCallback,fail",Reset_Tag);
        self.smsButton.enabled=YES;
        [Tooler showHttpMsg:result description:description msg:@"获取验证码失败"];
        
    }
}
-(void)timeCutdown
{
    self.seconds--;
    if(self.seconds<=0&&self.timer!=NULL)
    {
        [self.timer invalidate];
        self.timeLable.hidden=YES;
        [self.smsButton setTitle:@"短信获取" forState:UIControlStateNormal];
        self.smsButton.enabled=YES;
        self.smsButton.backgroundColor=[UIColor colorWithRed:57/255.0 green:157/255.0 blue:244/255.0 alpha:1.0];
    }
    else
    {
        NSString *timestr=[NSString stringWithFormat:@"%d           ",self.seconds];
        [self.smsButton setTitle:timestr forState:UIControlStateNormal];
    }

}
- (IBAction)backgroundTaped:(id)sender {
    [self.phone resignFirstResponder];
    [self.smsCode resignFirstResponder];
}
-(void)showErrorMsg:(NSString *)msg
{
    self.registerBtn.enabled=YES;
    [self.indicator stopAnimating];
    [LeoMsg showMsg:msg];
}
- (IBAction)phoneTextFieldChange:(id)sender {
    if(self.phone.text.length>20)
        self.phone.text=[self.phone.text substringToIndex:20];
    self.phone.text=[Tooler validateNumber:self.phone.text];
}
- (IBAction)codeTextFieldChange:(id)sender {
    if(self.smsCode.text.length>6)
        self.smsCode.text=[self.smsCode.text substringToIndex:6];
    self.smsCode.text=[Tooler validateNumber:self.smsCode.text];
}

@end
